package sample.note;

import android.content.ContentValues;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
import android.widget.ToggleButton;

import androidx.appcompat.app.AppCompatActivity;

import com.google.android.material.floatingactionbutton.FloatingActionButton;

import java.util.Calendar;

import lombok.SneakyThrows;
import sample.note.storage.Repository;

import static sample.note.storage.Repository.COLUMN_TIMESTAMP;
import static sample.note.storage.Repository.COLUMN_TXT;

public class EditTaskActivity extends AppCompatActivity implements View.OnClickListener
{

   EditText input;
   Repository repo;
   FloatingActionButton help;
   TextView time, date;

   @SneakyThrows
   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      repo = new Repository(this);

      setContentView(R.layout.activity_edit_task);
      input = findViewById(R.id.editText);

      help = findViewById(R.id.help);
      // context menu
      //registerForContextMenu(help);
      help.setOnClickListener(this);

      timePicker();
      repeat();
      deadline();
   }
   void deadline(){
      TextView dTxt = findViewById(R.id.deadlineTxt);

      final Calendar c = Calendar.getInstance();
      int mYear = c.get(Calendar.YEAR);
      int mMonth = c.get(Calendar.MONTH);
      int mDay = c.get(Calendar.DAY_OF_MONTH);
      DatePicker deadline = findViewById(R.id.deadline);
      deadline.init(mYear, mMonth, mDay, (view, year, monthOfYear, dayOfMonth) -> dTxt.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year));
   }

   void frequency(){
      RadioGroup g = findViewById(R.id.frequency);
      TextView fTxt = findViewById(R.id.frequencyTxt);

      g.setOnCheckedChangeListener((radioGroup, i) -> {
         RadioButton b = findViewById(i);
         fTxt.setText(b.getText());
      });

   }

   void repeat(){
      ToggleButton toggle = findViewById(R.id.toggle);
      DatePicker datePicker = findViewById(R.id.deadline);
      /*
      toggle.setOnClickListener(view -> {
         if(toggle.isChecked() ){
            datePicker.setVisibility(View.VISIBLE);
         }else{
            datePicker.setVisibility(View.GONE);
         }
      });
      */

      //viewTrigger(findViewById(R.id.repeat), findViewById(R.id.repeatConf));

      frequency();
   }

   void timePicker(){
      time = findViewById(R.id.time);
      date= findViewById(R.id.date);
      // Get Current Time
      final Calendar c = Calendar.getInstance();
      int mHour = c.get(Calendar.HOUR_OF_DAY);
      int mMinute = c.get(Calendar.MINUTE);
      int mYear = c.get(Calendar.YEAR);
      int mMonth = c.get(Calendar.MONTH);
      int mDay = c.get(Calendar.DAY_OF_MONTH);
      date.setText(mDay + "-" + (mMonth + 1) + "-" + mYear);
      time.setText(mHour + ":" + mMinute);

      DatePicker pickDate = findViewById(R.id.pickDate);
      TimePicker pickTime = findViewById(R.id.pickTime);

      View[] target = new View[]{pickDate, pickTime};
      View[] trigger = new View[]{date, time};
      for(int i=0; i<target.length; i++){
         viewTrigger(trigger[i], target[i]);
      }

      pickDate.init(mYear, mMonth, mDay, (view, year, monthOfYear, dayOfMonth) -> date.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year));
      pickTime.setIs24HourView(true);
      pickTime.setOnTimeChangedListener((view, hourOfDay, minute) -> time.setText(hourOfDay + ":" + minute));
   }

   void viewTrigger(View trigger, View v){
      trigger.setOnClickListener(view -> {
         if (v.getVisibility() == View.GONE) {
            v.setVisibility(View.VISIBLE);
         } else {
            v.setVisibility(View.GONE);
         }
      });
   }

   @Override
   public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
      super.onCreateContextMenu(menu, v, menuInfo);
      MenuInflater inflater = getMenuInflater();
      inflater.inflate(R.menu.list_menu, menu);
      menu.setHeaderTitle("Please Select:");
   }

   @SneakyThrows
   @Override
   public boolean onContextItemSelected(MenuItem item) {
      int selected = item.getItemId();

      if (selected == R.id.save) {
         return submit();
      }

      return false;
   }
   boolean submit(){
      String txt = input.getText().toString();
      if(txt.length() == 0){
         Toast.makeText(this, "Empty!", Toast.LENGTH_LONG).show();
         return true;
      }
      ContentValues row= new ContentValues();
      row.put(COLUMN_TXT, txt);
      row.put(COLUMN_TIMESTAMP, String.valueOf(System.currentTimeMillis()));
      repo.insert(row);
      Toast.makeText(this, "Saved!", Toast.LENGTH_LONG).show();
      onBackPressed();
      return true;
   }
   @Override
   public void onClick(View v) {
      //openContextMenu(v);
      submit();
   }
}
